import os 
import yaml
from pydantic import BaseModel, Field

class AppSettings(BaseModel):
    """基本配置"""
    port: int = Field(..., description="The port to run the application on")
    auto_reload: bool = Field(..., description="Whether to auto reload the application")


class DeviceSettings(BaseModel):
    """设备配置"""
    name: str = Field(..., description="The name of the device to use")

class ModelsSettings(BaseModel):
    """模型配置"""
    embedding_path: str = Field(..., description="The path to the embedding model")
    reranker_path: str = Field(..., description="The path to the reranker model")
    embedding_max_length: int = Field(..., description="The max length of the embedding")
    chat_model_url: str = Field(..., description="The url of the chat model")
    chat_model_key: str = Field(..., description="The key of the chat model")
    chat_model_name: str = Field(..., description="The name of the chat model")

class KnowledgeSettings(BaseModel):
    """知识库配置"""
    csv_path: str = Field(..., description="The path to the csv file")
    faiss_path: str = Field(..., description="The path to the faiss file")


class Settings(BaseModel):
    """全局配置"""
    app: AppSettings
    device: DeviceSettings
    models: ModelsSettings
    knowledge: KnowledgeSettings


def get_config() -> Settings:
    """获取配置"""
    env = os.getenv("ENV", "dev").lower()
    file_path = f"configs/envs/{env}.yaml"
    with open(file_path, "r") as f:
        raw_config = yaml.safe_load(f)
    return Settings(**raw_config)

config = get_config()